package com.example.blueconn.fragment;

import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;

import com.example.blueconn.R;
import com.example.blueconn.data.LightData;
import com.example.blueconn.databinding.FragmentHomeBinding;
import com.example.blueconn.fragment.listener.CallListener;
import com.example.blueconn.manager.DataMgr;
import com.example.blueconn.manager.FastBleManager;
import com.example.blueconn.utils.DataUtils;
import com.example.blueconn.utils.ThreadPoolUtils;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends Fragment {
    public static final String TAG = "HomeFragment";

    private FragmentHomeBinding homeBinding;
    private CallListener callListener;

    public static HomeFragment newInstance() {
        HomeFragment fragment = new HomeFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    public void setCallListener(CallListener callListener) {
        this.callListener = callListener;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        homeBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_home, container, false);
        homeBinding.setLifecycleOwner(this);
        initView();
        initListener();
        return homeBinding.getRoot();
    }

    private void initView() {
//        homeBinding.tipMsg.setMovementMethod(ScrollingMovementMethod.getInstance());
//        DataMgr.refresh.observe(getViewLifecycleOwner(), new Observer<Boolean>() {
//            @Override
//            public void onChanged(Boolean aBoolean) {
//                homeBinding.tipMsg.setText(DataMgr.sb);
//            }
//        });

        homeBinding.addData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ThreadPoolUtils.submit(new Runnable() {
                    @Override
                    public void run() {
                        for (int i = 0; i < 6; i++) {
                            String rawData = "DD3" + (i+1) + "31302E303534373934202D31312E373532FF";
                            DataMgr.getInstance().postRawData(rawData);
                            try {
                                Thread.sleep(100);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }

                        for (int i = 0; i < 5; i++) {
                            String rawData = "DD4" + (i+1) + "302E303534373934202D31312E37353232FF";
                            DataMgr.getInstance().postRawData(rawData);
                            try {
                                Thread.sleep(100);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        while (true) {
                            try {
                                DataMgr.getInstance().postRawData(DataUtils.randomLightOne());
                                Thread.sleep(100);
                                DataMgr.getInstance().postRawData(DataUtils.randomLightTwo());
                                Thread.sleep(100);
                                DataMgr.getInstance().postRawData(DataUtils.randomTemp());
                                Thread.sleep(100);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });

                ThreadPoolUtils.submit(new Runnable() {
                    @Override
                    public void run() {

                    }
                });
            }
        });

        DataMgr.getRealLightData().observe(getViewLifecycleOwner(), lightData -> {
            homeBinding.lightA1.setText(String.valueOf(lightData.lightA1));
            homeBinding.lightA2.setText(String.valueOf(lightData.lightA2));
            homeBinding.lightB1.setText(String.valueOf(lightData.lightB1));
            homeBinding.lightB2.setText(String.valueOf(lightData.lightB2));
            homeBinding.lightC1.setText(String.valueOf(lightData.lightC1));
            homeBinding.lightC2.setText(String.valueOf(lightData.lightC2));
            homeBinding.lightD1.setText(String.valueOf(lightData.lightD1));
            homeBinding.lightD2.setText(String.valueOf(lightData.lightD2));
            homeBinding.lightE1.setText(String.valueOf(lightData.lightE1));
            homeBinding.lightE2.setText(String.valueOf(lightData.lightE2));
            homeBinding.lightF1.setText(String.valueOf(lightData.lightF1));
            homeBinding.lightF2.setText(String.valueOf(lightData.lightF2));

//            DataMgr.sb.append("\n").append("L1: ").append(lightData.light1).append("\n")
//                    .append("L2:").append(lightData.light2).append("\n")
//                    .append("L3:").append(lightData.light3).append("\n")
//                    .append("L4:").append(lightData.light4).append("\n")
//                    .append("L5").append(lightData.light5).append("\n")
//                    .append("L6").append(lightData.light6).append("\n");
        });

        DataMgr.getRealTempData().observe(getViewLifecycleOwner(), tempData -> {
            homeBinding.tempA.setText(String.valueOf(tempData.temp1));
            homeBinding.tempB.setText(String.valueOf(tempData.temp2));
            homeBinding.tempC.setText(String.valueOf(tempData.temp3));
            homeBinding.tempD.setText(String.valueOf(tempData.temp4));
            homeBinding.tempE.setText(String.valueOf(tempData.temp5));
//            DataMgr.sb.append("\n").append("T1: ").append(tempData.temp1).append("\n")
//                    .append("T2:").append(tempData.temp2).append("\n")
//                    .append("T3:").append(tempData.temp3).append("\n")
//                    .append("T4:").append(tempData.temp4).append("\n")
//                    .append("T5").append(tempData.temp5).append("\n");
        });
    }

    private void initListener() {
        homeBinding.openDetectMode.setOnClickListener(v -> {
            FastBleManager.getInstance().writeData(DataUtils.openDetectMode());

        });
        homeBinding.openTempMode.setOnClickListener(v -> {
            FastBleManager.getInstance().writeData(DataUtils.openTempMode());
        });

        homeBinding.lightAA.setOnClickListener(v -> {
            callJump(0, true);
        });
        homeBinding.lightBA.setOnClickListener(v -> {
            callJump(1, true);
        });
        homeBinding.lightCA.setOnClickListener(v -> {
            callJump(2, true);
        });
        homeBinding.lightDA.setOnClickListener(v -> {
            callJump(3, true);
        });
        homeBinding.lightEA.setOnClickListener(v -> {
            callJump(4, true);
        });
        homeBinding.lightFA.setOnClickListener(v -> {
            callJump(5, true);
        });
        homeBinding.tempAA.setOnClickListener(v -> {
            callJump(0, false);
        });
        homeBinding.tempBA.setOnClickListener(v -> {
            callJump(1, false);
        });
        homeBinding.tempCA.setOnClickListener(v -> {
            callJump(2, false);
        });
        homeBinding.tempDA.setOnClickListener(v -> {
            callJump(3, false);
        });
        homeBinding.tempEA.setOnClickListener(v -> {
            callJump(4, false);
        });
    }

    private void callJump(int number, boolean isLight) {
        if (callListener != null) {
            callListener.callMethod(1, number, isLight);
        }
    }
}